package cn.flexui.flexuiserver.controller.web;

import cn.dev33.satoken.stp.StpUtil;
import cn.flexui.flexuiserver.common.ResponseResultGenerator;
import cn.flexui.flexuiserver.common.convert.SessionRecordConvert;
import cn.flexui.flexuiserver.domain.ResponseResult;
import cn.flexui.flexuiserver.domain.entity.BaseEntity;
import cn.flexui.flexuiserver.domain.entity.SessionRecord;
import cn.flexui.flexuiserver.domain.vo.SessionRecordVO;
import cn.flexui.flexuiserver.service.SessionRecordService;
import cn.flexui.flexuiserver.utils.IdGenerate;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 会话记录 前端控制器
 * </p>
 *
 * @author apeto
 * @since 2024-09-17 07:08:21
 */
@Tag(name = "会话记录")
@RestController
@RequestMapping("/sessionRecord")
public class SessionRecordController {

    @Autowired
    private SessionRecordService sessionRecordService;
    @Autowired
    private SessionRecordConvert sessionRecordConvert;

    @Operation(summary = "保存或修改")
    @PostMapping(value = "/saveOrUpdate")
    public ResponseResult<String> insertOrUpdate() {
        return ResponseResultGenerator.success(IdGenerate.getId());
    }

    @Operation(summary = "会话列表")
    @Parameter(name = "size", description = "每页显示条数，默认 10")
    @Parameter(name = "current", description = "当前页，默认 1")
    @GetMapping("/list")
    public ResponseResult<List<SessionRecordVO>> list() {
        long userId = StpUtil.getLoginIdAsLong();

        LambdaQueryWrapper<SessionRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SessionRecord::getUserId, userId);
        queryWrapper.orderByDesc(BaseEntity::getId);
        queryWrapper.last("limit 50");
        List<SessionRecord> list = sessionRecordService.list(queryWrapper);

        return ResponseResultGenerator.success(sessionRecordConvert.toVOList(list));
    }

}
